package com.aifeng.hassan_food.login

import android.os.Bundle
import android.text.InputFilter
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.getExtra
import com.aifeng.utils.subscribeByNoErrorWithException
import com.aifeng.viewmodel.LoginViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_register_done.*
import org.jetbrains.anko.toast
import org.json.JSONObject
import org.koin.androidx.viewmodel.ext.android.getViewModel

class RegisterDoneActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register_done)
        initTitle(title = "注册完成")
    }

    override fun getViewModel() = getViewModel<LoginViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        register_name.filters = arrayOf<InputFilter>(NameLengthFilter(20))
    }

    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_done -> {
                when {
                    register_pwd.text.isBlank() -> {
                        register_pwd.requestFocus()
                        toast("请输入新密码")
                        return
                    }
                    register_confirm.text.isBlank() -> {
                        register_confirm.requestFocus()
                        toast("请再次输入新密码")
                        return
                    }
                    register_name.text.isBlank() -> {
                        register_name.requestFocus()
                        toast("请输入自己的店铺名称")
                        return
                    }
                    !register_pwd.text.isPasswordWithAndNumber() -> {
                        toast("密码应设置为6~20位字母加数字组合")
                        return
                    }
                    register_pwd.textString() != register_confirm.textString() -> {
                        toast("密码输入不一致，请重新输入")
                        return
                    }
                }

                getViewModel().getFinish(
                    "inviteCode" to intent.getExtra("code"),
                    "phone" to intent.getExtra("phone"),
                    "password" to register_pwd.textString(),
                    "shopname" to register_name.textString()
                ).bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoErrorWithException(
                        event = getViewModel(),
                        onSuccess = {
                            val obj = JSONObject(it)
                            putShared("phone", obj.optNotNull("phone"))
                            putShared("userId", obj.optNotNull("id"))
                            putShared("nickName", obj.optNotNull("nick"))
                            putShared("shopName", obj.optNotNull("shopname"))
                            putShared("headImg", obj.optNotNull("headimg"))
                            putShared("shopId", obj.optNotNull("shopid"))
                            putShared("shopCode", obj.optNotNull("shopqrcode"))
                            putShared("balance", obj.optNotNull("balance"))
                            putShared("openId", obj.optNotNull("wx_open_id"))

                            putShared("realName", obj.optNotNull("realname"))
                            putShared("bankCard", obj.optNotNull("bankCardNumber"))
                            putShared("bindId", obj.optNotNull("bind_wx_id"))
                            putShared("bindImg", obj.optNotNull("bind_wx_img"))
                            putShared("bindName", obj.optNotNull("bind_wx_nick"))

                            putShared("createData", obj.optNotNull("upgradedate"))

                            isLogin = true
                            LiveEventBus.get("loginStatus").post("login")
                            LiveEventBus.get("mainLogin").post("refresh")
                            LiveEventBus.get("otherLogin").post("refresh")
                            ActivityStack.screenManager.popActivities(
                                LoginActivity::class.java,
                                RegisterCodeActivity::class.java,
                                RegisterPhoneActivity::class.java,
                                this@RegisterDoneActivity::class.java
                            )
                        },
                        onException = {
                            ActivityStack.screenManager.popActivities(
                                RegisterCodeActivity::class.java,
                                RegisterPhoneActivity::class.java,
                                this@RegisterDoneActivity::class.java
                            )
                        }
                    )
            }
        }
    }

}
